package com.ruanda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.ProdProp;
import com.ruanda.domain.ProdPropValue;
import com.ruanda.model.Result;
import com.ruanda.service.ProdPropService;
import com.ruanda.service.ProdPropValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "商品规格接口管理")
@RequestMapping("prod/spec")
@RestController
public class ProdSpecController {

    @Autowired
    private ProdPropService prodPropService;
    @Autowired
    private ProdPropValueService prodPropValueService;

    /**
     * 多条件分页查询商品规格
     * @param current 当前页
     * @param size 每页大小
     * @param propName 规格名称
     * @return
     */
    @Operation(summary = "多条件分页查询商品规格")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProdSpecPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String propName) {
        Page<ProdProp> page = prodPropService.queryProdSpecPage(current, size, propName);
        return Result.success(page);
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProdSpec(@RequestBody ProdProp prodProp) {
        Integer count = prodPropService.saveProdSpec(prodProp);
        return Result.handle(count>0);
    }

    @Operation(summary = "修改商品规格")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> modifyProdSpec(@RequestBody ProdProp prodProp) {
        Integer count = prodPropService.modifyProdSpec(prodProp);
        return Result.handle(count>0);
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("{propId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> removeProdSpec(@PathVariable Long propId) {
        Integer count = prodPropService.removeProdSpec(propId);
        return Result.handle(count>0);
    }

    @Operation(summary = "查询商品属性列表")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> loadProdPropList() {
        List<ProdProp> list = prodPropService.queryProdPropList();
        return Result.success(list);
    }
    

    @Operation(summary = "根据商品属性标识查询属性值集合")
    @GetMapping("listSpecValue/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdPropValue>> loadProdPropValues(@PathVariable Long propId) {
        List<ProdPropValue> list = prodPropValueService.list(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId, propId)
        );
        return Result.success(list);
    }
}